//package org.celery.modules.ticket.task;
//
//import com.alibaba.ocean.rawsdk.ApiExecutor;
//import com.alibaba.ocean.rawsdk.client.APIId;
//import com.alibaba.ocean.rawsdk.client.entity.AuthorizationToken;
//import com.alibaba.ocean.rawsdk.common.SDKResult;
//import com.alibaba.trade.param.*;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.celery.common.config.AlibabaProperties;
//import org.celery.modules.shop.entity.Shop;
//import org.celery.modules.shop.service.IShopService;
//import org.celery.modules.ticket.dto.TicketDTO;
//import org.celery.modules.ticket.service.ITicketService;
//import org.springblade.core.tool.jackson.JsonUtil;
//import org.springblade.core.tool.utils.Func;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.text.ParseException;
//import java.time.Duration;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// * 订单同步任务
// *
// * @author luoqc
// */
//@Component
//@AllArgsConstructor
//@Slf4j
//public class TicketSyncTask {
//
//    private final IShopService shopService;
//    private final ITicketService ticketService;
//    private final AlibabaProperties alibabaProperties;
//
//    @Scheduled(cron = "0 */5 * * * ? ")
//    public void scheduled() {
//        LocalDateTime startTime = LocalDateTime.now();
//        log.info("开始时间: " + startTime);
//
//        List<Shop> shopList = shopService.list();
//        shopList.forEach(this::process);
//
//        LocalDateTime endTime = LocalDateTime.now();
//        log.info("结束时间: " + startTime + ", 总耗时: " + Duration.between(startTime, endTime).toMillis() + "毫秒");
//    }
//
//    void process(Shop shop) {
//        ApiExecutor apiExecutor = new ApiExecutor(alibabaProperties.getAppKey(), alibabaProperties.getAppSecret());
//        AlibabaTradeGetSellerOrderListParam param = new AlibabaTradeGetSellerOrderListParam();
//        param.setOceanApiId(new APIId("com.alibaba.trade", "alibaba.trade.ec.getOrderList.sellerView", 1));
//        param.setPage(1);
//        param.setPageSize(100);
//
//        SDKResult<AlibabaTradeGetSellerOrderListResult> result = apiExecutor.execute(param, shop.getAccessToken());
//
//        if (Func.isNotEmpty(result.getErrorCode()) && result.getErrorCode().equals("gw.NeedUserAuthenticated")) {
//            AuthorizationToken authorizationToken = apiExecutor.refreshToken(shop.getRefreshToken());
//            shop.setAccessToken(authorizationToken.getAccess_token());
//            shopService.saveOrUpdate(shop);
//            result = apiExecutor.execute(param, shop.getAccessToken());
//        }
//
//        if (result.getResult().getSuccess()) {
//            AlibabaOpenplatformTradeModelTradeInfo[] resultList = result.getResult().getResult();
//            List<TicketDTO> ticketDTOS = Arrays.stream(resultList).map(alibabaOpenPlatformTradeModelTradeInfo -> new TicketDTO() {{
//                setBaseInfo(JsonUtil.toMap(JsonUtil.toJson(alibabaOpenPlatformTradeModelTradeInfo.getBaseInfo())));
////                setExtAttributes(JsonUtil.toMap(JsonUtil.toJson(alibabaOpenPlatformTradeModelTradeInfo.getExtAttributes())));
//                setNativeLogistics(JsonUtil.toMap(JsonUtil.toJson(alibabaOpenPlatformTradeModelTradeInfo.getNativeLogistics())));
//                setOrderStatus(alibabaOpenPlatformTradeModelTradeInfo.getBaseInfo().getStatus());
//                setOfficialLogisticOrder(String.valueOf(alibabaOpenPlatformTradeModelTradeInfo.getOfficialLogisticOrder()));
//                setOrderRateInfo(JsonUtil.toMap(JsonUtil.toJson(alibabaOpenPlatformTradeModelTradeInfo.getOrderRateInfo())));
//
//                List<Map> products = new ArrayList<>();
//                for (AlibabaOpenplatformTradeModelProductItemInfo productItem : alibabaOpenPlatformTradeModelTradeInfo.getProductItems()) {
//                    products.add(JsonUtil.toMap(JsonUtil.toJson(productItem)));
//                }
//                setProductItems(products);
//
//                List<Map> termItems = new ArrayList<>();
//                for (AlibabaOpenplatformTradeModelTradeTermsInfo termItem : alibabaOpenPlatformTradeModelTradeInfo.getTradeTerms()) {
//                    products.add(JsonUtil.toMap(JsonUtil.toJson(termItem)));
//                }
//                setTradeTerms(termItems);
//            }}).collect(Collectors.toList());
//
//            try {
//                ticketService.addOrderInput(ticketDTOS);
//            } catch (ParseException e) {
//                log.error(e.getMessage());
//            }
//        }
//    }
//}
